#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 23:47
# @Author  : when
# @File    : page.py
# @Description : 已爬取的page模型

from sqlalchemy.orm import relationship
from sqlalchemy import Column, Integer, String, TEXT

from crawler.orm_model.base_model import Base
from crawler.orm_model.base_model import BaseModel
from crawler.orm_model.boss_job import BossJob
from crawler.orm_model.page_boss_job import PageBossJob


class PageStatusChoice:
    INIT = "0"  # 待爬取
    FINISH = "1"  # 爬取成功
    FAIL = "2"  # 爬取失败
    RESOLVER_FINISH = "3"  # 解析成功
    RESOLVER_FAIL = "4"  # 解析失败


class PageTypeChoice:
    BOSS_JOB_INFO = "1"  # boss直聘job展示页面
    BOSS_JOB_DETAILS = "2"  # boss直聘job详情页面


class Page(Base, BaseModel):
    """
    爬取页面对象
    """
    __tablename__ = 'page'

    id = Column(Integer, primary_key=True, autoincrement=True)
    # 爬取url
    url = Column(String(255))
    # 爬取成功：网页源码、爬取失败：失败原因
    html = Column(TEXT)
    # 页面类型
    type = Column(String(255))
    # 状态
    status = Column(String(255), default=PageStatusChoice.INIT)

    # 关联外键(back_populates反查名称)
    page_boss_job = relationship("PageBossJob", back_populates='page')
